<?php

class email
{
	var $profile;
	
	var $data;
	var $valid = true;
	
	var $email = Array();
	var $replacements = Array();

	function email($profile, $data)
	{
		global $lang, $www_path, $skin_dir;
	
		$this->profile = $profile;
		$this->data = $data;
		
		if($profile == 'event_reminder')
		{
			$this->email['title']="{CHAMPIONSHIP}";
			$this->email['subTitle']="{ROUNDNAME} {TRACKNAME}<BR>{LOCATION}<BR>{DATE}";
			$this->email['emailTitle']= "{CHAMPIONSHIP2} {ROUNDNAME} {TRACKNAME}";
			$this->email['to']="{EMAILADRESS}";
			$this->email['image1']="{CHAMPIONSHIPLOGO}";
			$this->email['image2']="{TRACKLOGO}";
			$this->email['logo']='<img src="' . $www_path . $skin_dir . 'images/email_grc_logo.jpg"/>';
			$this->email['message']= $lang['EmailEventReminderMessage'] . "<p>" . $lang['EmailNotification'] . "</p><p>" . $lang['EmailFooter'] . '</p>';
		}
		else if($profile == 'signedup_reminder')
		{
			$this->email['title']="{CHAMPIONSHIP}";
			$this->email['subTitle']="{ROUNDNAME} {TRACKNAME}<BR>{LOCATION}<BR>{DATE}";
			$this->email['emailTitle']= "{CHAMPIONSHIP2} {ROUNDNAME} {TRACKNAME}";
			$this->email['to']="{EMAILADRESS}";
			$this->email['image1']="{CHAMPIONSHIPLOGO}";
			$this->email['image2']="{TRACKLOGO}";
			$this->email['logo']='<img src="' . $www_path . $skin_dir . 'images/email_grc_logo.jpg"/>';
			$this->email['message']= $lang['EmailSignedupReminderMessage'] . "<p>" . $lang['EmailNotification'] . "</p><p>" . $lang['EmailFooter'] . '</p>';
		}
		else if($profile == 'incident_adminedit')
		{
			$this->email['title']=$lang['IncidentAdminEmailEdited'];
			$this->email['subTitle']="";
			$this->email['emailTitle']= $lang['IncidentAdminEmailEdited'];
			$this->email['to']="{EMAILADRESS}";
			$this->email['image1']="";
			$this->email['image2']="";
			$this->email['logo']='<img src="' . $www_path . $skin_dir . 'images/email_grc_logo.jpg"/>';
			$this->email['message']= $lang['IncidentAdminEmailEditedMessage'] . "<p>" . $lang['EmailFooter'] . '</p>';
		}
		else if($profile == 'incident_adminadd')
		{
			$this->email['title']=$lang['IncidentAdminEmailEdited'];
			$this->email['subTitle']="";
			$this->email['emailTitle']= $lang['IncidentAdminEmailEdited'];
			$this->email['to']="{EMAILADRESS}";
			$this->email['image1']="";
			$this->email['image2']="";
			$this->email['logo']='<img src="' . $www_path . $skin_dir . 'images/email_grc_logo.jpg"/>';
			$this->email['message']= $lang['IncidentAdminEmailAddedMessage'] . "<p>" . $lang['EmailFooter'] . '</p>';
		}
		else if($profile == 'incident_admindelete')
		{
			$this->email['title']=$lang['IncidentAdminEmailEdited'];
			$this->email['subTitle']="";
			$this->email['emailTitle']= $lang['IncidentAdminEmailEdited'];
			$this->email['to']="{EMAILADRESS}";
			$this->email['image1']="";
			$this->email['image2']="";
			$this->email['logo']='<img src="' . $www_path . $skin_dir . 'images/email_grc_logo.jpg"/>';
			$this->email['message']= $lang['IncidentAdminEmailDeletedMessage'] . "<p>" . $lang['EmailFooter'] . '</p>';
		}
		else if($profile == 'incident_adminstatus')
		{
			$this->email['title']=$lang['IncidentAdminEmailEdited'];
			$this->email['subTitle']="";
			$this->email['emailTitle']= $lang['IncidentAdminEmailEdited'];
			$this->email['to']="{EMAILADRESS}";
			$this->email['image1']="";
			$this->email['image2']="";
			$this->email['logo']='<img src="' . $www_path . $skin_dir . 'images/email_grc_logo.jpg"/>';
			$this->email['message']= $lang['IncidentAdminEmailStatusMessage'] . "<p>" . $lang['EmailFooter'] . '</p>';
		}
		else if($profile == 'incident_adminpadded')
		{
			$this->email['title']=$lang['IncidentAdminEmailEdited'];
			$this->email['subTitle']="";
			$this->email['emailTitle']= $lang['IncidentAdminEmailEdited'];
			$this->email['to']="{EMAILADRESS}";
			$this->email['image1']="";
			$this->email['image2']="";
			$this->email['logo']='<img src="' . $www_path . $skin_dir . 'images/email_grc_logo.jpg"/>';
			$this->email['message']= $lang['IncidentAdminEmailPublicAddedMessage'] . "<p>" . $lang['EmailFooter'] . '</p>';
		}
		else if($profile == 'incident_created')
		{
			$this->email['title']=$lang['IncidentEmailCreated'];
			$this->email['subTitle']="";
			$this->email['emailTitle']= $lang['IncidentEmailCreated'];
			$this->email['to']="{EMAILADRESS}";
			$this->email['image1']="";
			$this->email['image2']="";
			$this->email['logo']='<img src="' . $www_path . $skin_dir . 'images/email_grc_logo.jpg"/>';
			$this->email['message']= $lang['IncidentEmailCreatedMessage'] . "<p>" . $lang['EmailFooter'] . '</p>';
		}
		else if($profile == 'team_join_request')
		{
			$this->email['title']=$lang['TeamJoinEmailTitle'];
			$this->email['subTitle']="";
			$this->email['emailTitle']= $lang['TeamJoinEmailTitle'];
			$this->email['to']="{EMAILADRESS}";
			$this->email['image1']='<img src="' . $www_path . $skin_dir . 'images/email_grc_logo_small.jpg"/>';
			$this->email['image2']="{TEAM_LOGO}";
			$this->email['logo']='';
			$this->email['message']= $lang['TeamJoinEmailBody'] . "<p>" . $lang['EmailFooter'] . '</p>';
		}
		else if($profile == 'warning_near_ban')
		{
			$this->email['title']=$lang['EmailWarningBanNearTitle'];
			$this->email['subTitle']="";
			$this->email['emailTitle']= $lang['EmailWarningBanNearTitle'];
			$this->email['to']="{EMAILADRESS}";
			$this->email['image1']='';
			$this->email['image2']='';
			$this->email['logo']='<img src="' . $www_path . $skin_dir . 'images/email_grc_logo_small.jpg"/>';
			$this->email['message']= $lang['EmailWarningBanNearMessage'] . "<p>" . $lang['EmailFooter'] . '</p>';
		}
		else if($profile == 'driver_ban')
		{
			$this->email['title']=$lang['EmailBannedTitle'];
			$this->email['subTitle']="";
			$this->email['emailTitle']= $lang['EmailBannedTitle'];
			$this->email['to']="{EMAILADRESS}";
			$this->email['image1']='';
			$this->email['image2']='';
			$this->email['logo']='<img src="' . $www_path . $skin_dir . 'images/email_grc_logo_small.jpg"/>';
			$this->email['message']= $lang['EmailBannedMessage'] . "<p>" . $lang['EmailFooter'] . '</p>';
		}
		else if($profile == 'admin_massemail')
		{
			$this->email['title']="{TITLE}";
			$this->email['subTitle']="";
			$this->email['emailTitle']= "{TITLE}";
			$this->email['to']="{EMAILADRESS}";
			$this->email['image1']="";
			$this->email['image2']="";
			$this->email['logo']='<img src="' . $www_path . $skin_dir . 'images/email_grc_logo.jpg"/>';
			$this->email['message']="<p>{$lang['EmailIntroduction']}</p><p>{MESSAGE}</p><p>{$lang['EmailFooter']}</p><p>{$lang['EmailNotification']}</p>";
		}
		else
		{
			$this->valid = false;
		}
		
	}
	
	function addReplacement($key, $string, $data=0)
	{
		if($data == 1)
		{
			$this->replacements[$key] = $this->data[$string];
		}
		else
		{
			$this->replacements[$key] = $string;
		}
	}
	
	function render()
	{
		global $skin_dir, $www_path;
		
		foreach($this->email as $emailItem => $emailValue)
		{
			foreach($this->replacements as $replaceKey => $replaceValue)
			{
				$this->email[$emailItem] = str_replace($replaceKey, $replaceValue, $this->email[$emailItem]);
			}
		}
		
		$email = $this->email;
		include $skin_dir . 'email/template.tpl';
	}
	
	function sendEmail()
	{	
		if($this->valid)
		{
			ob_start();
			$this->render();
			$body = ob_get_contents();
			ob_end_clean();
			$to = $this->email['to'];
			$subject = $this->email['emailTitle'];
			$headers = "From: GRC League Control <noreply@rfgrc.co.uk>\r\nX-Mailer: php\r\n"
							 . "Reply-To: noreply@rfgrc.co.uk\r\n"
							 . "Content-type: text/html; charset=iso-8859-1\r\n";
      		//echo $to . '<br>' . $subject . '<br>' . $body. '<br>';
			mail($to, $subject, $body, $headers);
		}
	}
}

?>